// pages/edit_dev/edit_page.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text_area:'',
    deviceid:'',
    group:[],
    group_cur_index:0,
    deviceid:'',   
    devClassId:'',
    devicename:'',  //设备名就是描述
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    that.setData({
      deviceid: options.deviceid,
   
      devClassId: options.devClassId,
      devicename: options.devicename
    });
    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    };

    wx.request({
      url: 'https://119.3.46.145/server/FindGroup?userid='+app.globalData.userid,
      method:'post',
      header:header,
      success: res => {
        let group = JSON.parse(res.data.data); 
        let tmp_index = 0;

        group.forEach(function(item, index, self){
          if(item.groupId == that.data.devClassId){
            tmp_index = index;
            return false;
          }
        });
       
        that.setData({
          group: group,
          group_cur_index: tmp_index
        });


      },
      fail:error=>{

      }
    });
  },  

  bindTextAreaBlur:function(e){
    let that = this;

    that.setData({
      devicename: e.detail.value
    });
  },

  bindPickerChange:function(e){
    //console.log(e, "e bindPickerChange");
    //e.detail.value表示数组的下标 index 0,1,2,3
    let that = this;   

    let tmp_groupId = that.data.group[e.detail.value].groupId;

    that.setData({
      devClassId: tmp_groupId
    });
  },

  modify_button:function(){
    let that = this;

    //console.log(that.data, "that.data");

    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    };

    wx.request({
      url: 'https://119.3.46.145/server/UpdateDevice?userid=' + app.globalData.userid + '&deviceid=' + that.data.deviceid + '&description=' + that.data.devicename + '&devClassId=' + that.data.devClassId,
      method: 'post',
      header: header,
      success: res => {      
        wx.showModal({
          title: '提示',
          content: '修改设备成功'
        });

        //跟新一下 全局变量  app.globalData.dev_list 获取最新的设备列表 在请求getLastInfo的时候用
        //方式一  直接请求最新的
        wx.request({
          url: "https://119.3.46.145/server/FindDevice?userid=" + app.globalData.userid,
          type: "post",
          header: header,
          success: res => {
            app.globalData.edit_del_add_dev_flag = true; //修改了设备，标记改为 true 需要重新设置地图的 center 以及scale
            //如果没有设备 则return
            if (res.data == '') {
              return;
            }

            let devices_data = JSON.parse(res.data.devices);

            if (devices_data.length != 0) {
              app.globalData.dev_list = devices_data;
            }
          },
          fail: function (error) {

          }
        });

        //方式二 修改数组 暂未写

        let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
        let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值
       
        
        prevPage.changeData(that.data.devClassId, that.data.deviceid, that.data.devicename);
        wx.navigateBack(); //关闭当前页面，返回上一个页面
      },
      fail: error => {

      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})